package load.storage;

import load.parent.Store;
import core.ConnectException;
import core.DispatcherPort;
import core.InPort;
import core.OutPort;
import core.TriggerPort;

/**
 *
 * @author Wijnand, modified by Ben
 */
public class StoreIO<T> extends Store<T> {
    private DispatcherPort valueChanged = new DispatcherPort();

    private TriggerPort argChanged = new TriggerPort() {
        @Override
        public void callInternal() {
            setValue((T)invalue.getValue());
        }
    };
    private InPort invalue;

    public StoreIO(T x) {
        super(x);
        invalue = new InPort(_value.getClass(), argChanged);
        this.autoPutPorts();
    }

    @Override
    public void setValue(T x) throws ConnectException {
        super.setValue(x);
        valueChanged.callTargets();
    }

    @Override
    public void setType(T x) throws ConnectException {
        this.setValue(x);
        value = new OutPort(_value.getClass(),valueChanged){	// T.class geeft fout
            @Override
            protected java.lang.Object getValueInternal() {
                return super.getValue();
            }
        };
        ports.put("value", value);
        invalue = new InPort(_value.getClass(), argChanged);
        ports.put("invalue", invalue);
    }
}